﻿using QWManage.Logs;
using QWManage.Model;
using System;
using System.Threading;

namespace QWManage.TimerTask
{
    public class GroupsThread
    {
        public static void Run()
        {
            try
            {
                LogMgr.Log("定群采集", "开始 OK");
                int minutes = 1;
                while (true)
                {
                    Thread.Sleep(1000 * 60);
                    AppConfigInfo cfg = AppCofingMgr.Get();
                    int maxMinutes = 0;
                    if (cfg.Groups)
                    {
                        int min = int.Parse(cfg.HourStart.ToString("00") + cfg.MinuteStart.ToString("00"));
                        int max = int.Parse(cfg.HourEnd.ToString("00") + cfg.MinuteEnd.ToString("00"));
                        int now = int.Parse(DateTime.Now.ToString("HHmm"));
                        if (now >= min && now <= max)
                        {
                            maxMinutes = GroupsTask.Run(minutes);
                        }
                    }
                    minutes++;
                    if (maxMinutes != 0 && minutes > maxMinutes)
                        minutes = 1;
                }
            }
            catch (ThreadAbortException ex)
            {
                LogMgr.Log("定群采集", "ThreadAbortException", LogLevel.Error);
                LogMgr.Log(ex);
            }
            catch (Exception ex)
            {
                LogMgr.Log("定群采集", ex.Message, LogLevel.Error);
                LogMgr.Log(ex);
            }
            finally
            {
                LogMgr.Log("定群采集", "退出 OK");
            }
        }
    }
}
